<template>
  <div>
    <el-main>
      <el-table
        :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="userName" label="用户名" show-overflow-tooltip width="100"></el-table-column>
        <el-table-column prop="mobilePhone" label="用户手机" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column prop="passWord" label="用户密码" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column prop="role" label="身份" width="100" filter-placement="bottom-end"></el-table-column>
        <el-table-column prop="imageLocation" label="头像" width="180" show-overflow-tooltip filter-placement="bottom-end"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: -40px;float:left"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[6, 8, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
      ></el-pagination>
    </el-footer>
    <!-- dialog 删除 -->
    <el-dialog title="删除用户确认提示" :visible.sync="dialogDelete">
      <el-form :model="onejilu">
        <p>你确定要删除该删除用户吗?此操作将删除该用户的身份信息，服务器邮件信息，服务器历史分析记录，索引库邮件信息，索引库历史分析信息</p>
        <strong>
          邮件索引:index {{onejilu.index}},
          <br />
          用户名:--- {{onejilu.userName}},
          <br />
          用户密码：---{{onejilu.passWord}},
          <br />
          用户手机：---{{onejilu.mobilePhone}},
          <br />
          用户身份：---{{onejilu.role}},
        </strong>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDelete = false">取 消</el-button>
        <el-button type="primary" @click="dialogDelete = false;finalDelete(onejilu)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { emlPath } from "./config/config";
export default {
  data() {
    return {
      tableData: [],
      //分页
      currentPage: 1, //初始页
      pagesize: 6, //    初始每页的数据
      //dialog
      dialogDelete: false,
      onejilu: {}
    };
  },
  created() {
    this.getInit();
  },
  computed: {
    //进入的用户
    userSO_json() {
      let userSO_json = JSON.parse(sessionStorage.getItem("userSO_json"));
      return userSO_json;
    }
  },
  methods: {
    async getInit() {
      axios
        .get("/api/UserController/user/alluser", {
          headers: {
            //头部信息
            "Content-Type": "application/json;charset=utf-8",
            token: this.userSO_json.token
          }
        })
        .then(response => {
          let list = response.data.data;
          console.log(list);
          this.tableData=list;
        })
        .catch(error => {
        });
    },

    //分页
    // 初始每页数据数pagesize
    handleSizeChange(size) {
      this.pagesize = size;
    },
    //初始页currentPage
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
    },
    handleDelete(index, row) {
      this.dialogDelete = true;
      this.onejilu = row; //给后端删除条件
      this.onejilu.index = (this.currentPage - 1) * this.pagesize + index; //给前端删除条件
      console.log("this.onejilu",this.onejilu)
    },
    finalDelete() {
      //后端删除
      axios
        .get(
          "/api/UserController/user/deleteuser?userName=" +
            this.onejilu.userName,
          {
            headers: {
              //头部信息
              "Content-Type": "application/json;charset=utf-8",
              token: this.userSO_json.token
            }
          }
        )
        .then(response => {
          let result = response.data;
          if (result.code == 200) {
            this.$message({
              showClose: true,
              message: "恭喜你，这是一条删除成功消息",
              type: "success"
            });
          }
        })
        .catch(error => {
          console.log(error);
          alert("网络错误，不能访问");
        });
      //前端删除
      this.tableData.splice(this.onejilu.index, 1);
    }
  }
};
</script>

<style  scoped>
.el-footer {
  height: 40px;
  width: 100%;
  background-color: #ddd;
  position: fixed;
  bottom: 0;
}
</style>